//
//  AppDelegate.swift
//  Weibo
//
//  Created by Albert Zhu on 16/9/27.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        setupAdditions()
        
        return true
    }
}

extension AppDelegate {
    
    func loadAppInfo() {
        DispatchQueue.global().async {
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)!
            
            let data = NSData(contentsOf: url)
            
            let jsonPath = "main.json".az_appendDocumentDir()
            data?.write(toFile:jsonPath , atomically: true)
            
            debugPrint("应用程序加载完毕 \(jsonPath)")
            
            // 取得用户授权显示通知
            if #available(iOS 10.0, *) {
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isSuccess, error) in
                    
                }
            } else {
                let noti = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
                UIApplication.shared.registerUserNotificationSettings(noti)
            }
        }
    }
    
    func setupAdditions() {
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
    }
}

